package com.roger.xcontact;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

public class MainActivity extends Activity {

	private static final String TAG = MainActivity.class.getName();

	private XContactApplication xcontactApp;
	private AutoCompleteTextView userNameTextView;
	private EditText userPaswordEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Log.i(TAG, "MainActivity Has Started!");

		/**
		 * 
		 * Username AutoCompleteTextView
		 * 
		 */
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, new String[] {
						"Roger", "xzqttt", "xzqttt@gmail.com" });
		userNameTextView = (AutoCompleteTextView) findViewById(R.id.username_edit_text);
		userNameTextView.setAdapter(adapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**********************************
	 * Action Events
	 ***********************************/

	/** Called when the user touches the button */
	public void loginClick(View view) {
		// Do something in response to button click
		Log.i(TAG, "Login Button had pressed");
		doAnyThing(view);
	}

	/** Called when the user touches the button */
	public void registerClick(View view) {
		// Do something in response to button click
		Log.i(TAG, "Register Button had pressed");
		doAnyThing(view);
	}

	private final void doAnyThing(View view) {
		userNameTextView = (AutoCompleteTextView) findViewById(R.id.username_edit_text);
		userPaswordEditText = (EditText) findViewById(R.id.passwd_edit_text);

		String userName = "";
		if (userNameTextView != null) {
			userName = userNameTextView.getText().toString();
		}

		String userPasword = "";
		if (userPaswordEditText != null) {
			userPasword = userPaswordEditText.getText().toString();
		}

		xcontactApp = (XContactApplication) this.getApplication();
		xcontactApp.setUserName(userName);
		xcontactApp.setPasword(userPasword);

		Intent intent = new Intent(this, ResultActivity.class);
		startActivity(intent);
	}

}
